<?php

/**
 * Erra_Templating_Layout - Handles layouts
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Templating_Layout {

    public $pageParameters = array();
    public $views = array();
    public $view_parameters = array();
    public $tpl_file = '';
    public $ajaxcontent = false;

    public function __construct($tpl_file) {
        $this->tpl_file = $tpl_file;
    }

    /**
     * Erra_Templating_Layout::displayLayout() - Displays layout
     *
     * @return
     */
    public function displayLayout($urlArgs) {
        $layout_tpl_file = $this->tpl_file;
        $layout_file = Erra_Core_Lexer::getCleanPath(DOCUMENT_ROOT . '/application/Layouts/' . tpl_get_full_path($layout_tpl_file));

        $file_compilated = basename($layout_file) . '.compile.php';

        $compile_path = Erra_Core_Lexer::getCleanPath(DOCUMENT_ROOT . '/' . config_get("path.public_directory") . '/__cache/' . $file_compilated);

        $layout_contents = file_get_contents($layout_file);


        /*
         * Add barview and AJAX loading if needed
         */
        if (config_get('admin_bar') == 1) {
            $layout_contents = str_replace('</body>', '<include src="Admin_AdminBar.phtml" />' . "\n" . '</body>', $layout_contents);
        }
        $layout_contents = str_replace('</body>', '<include src="Admin_AjaxLoader.phtml" />' . "\n" . '</body>', $layout_contents);

        //if (config_get('production_mode') == 0) {
            $contents = Erra_Core_Lexer::ufmParse($layout_contents, $this);
            file_put_contents($compile_path, $contents);
        //}

        ob_start();
        include($compile_path);
        $result = ob_get_contents();
        ob_end_clean();

        return $result;
    }

    /**
     * Erra_Templating_Layout::displayRegion() - Displays the right view depending the layout
     *
     * @param mixed $regionName
     * @return
     */
    public function displayRegion($regionName) {
        $viewParameters = isset($this->view_parameters[$regionName]) ? $this->view_parameters[$regionName] : array();
        if (isset($this->views[$regionName])) {
            // for seo-frieldy url's
            $subView = isset($this->pageParameters['realRoute']['GET_Subview']) ? $this->pageParameters['realRoute']['GET_Subview'] : null;

            $this->views[$regionName]->pageParameters = $this->pageParameters;
            $view_data = '';
            if ($this->tpl_file == 'AjaxLayout.phtml' && !$this->ajaxcontent) {
                return $this->views[$regionName]->displayView($viewParameters);
            } else {
                return '<div id="' . $regionName . '" class="erra-region" data-view="' . basename($this->views[$regionName]->tpl_file, '.phtml') . '">' . $this->views[$regionName]->displayView($viewParameters) . '</div>';
            }
        }
    }

    /**
     * Erra_Templating_Layout::getRegionParameters() - Returns parameters relative to a region
     *
     * @param mixed $regionName
     * @return
     */
    public function getRegionParameters($regionName) {
        $viewParameters = isset($this->view_parameters[$regionName]) ? $this->view_parameters[$regionName] : array();
        if (isset($this->views[$regionName])) {
            return $viewParameters;
        }
    }

    /**
     * Erra_Templating_Layout::getRegionClass() - Returns the class relative to a region
     *
     * @param mixed $regionName
     * @return
     */
    public function getRegionClass($regionName) {
        $className = get_class($this->views[$regionName]);
        return $className;
    }

}